package edu.usc.cmb.iRegulome.action;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.opensymphony.xwork2.Action;

import edu.usc.cmb.iregulome.dao.SampleStudyDAO;
import edu.usc.cmb.iregulome.entity.Sample;
import edu.usc.cmb.iregulome.entity.json.Sample_JS;

public class TableBrowserAction extends ActionWithSampleStudyDAO {

	public TableBrowserAction(SampleStudyDAO sampleStudyDAO) {
		super(sampleStudyDAO);
	}
	

	private int totalStudyCount;

	private List<Sample_JS> samples;

	private List<String> cellTypeSet;

	private List<String> factorSet;

	private List<String> cellTypes;

	private List<String[]> studyIds;

	private String cellType;

	private String factor;

	private String studyId = "";

	private Integer taxonid;

	public void setCellTypes(List<String> cellTypes) {
		this.cellTypes = cellTypes;
	}

	public List<String> getCellTypes() {
		return cellTypes;
	}



	public List<String> getCellTypeSet() {
		return cellTypeSet;
	}

	public void setCellTypeSet(List<String> cellTypeSet) {
		this.cellTypeSet = cellTypeSet;
	}

	public List<String> getFactorSet() {
		return factorSet;
	}

	public void setFactorSet(List<String> factorSet) {
		this.factorSet = factorSet;
	}

	public List<Sample_JS> getSamples() {
		return samples;
	}

	public void setSamples(List<Sample_JS> samples) {
		this.samples = samples;
	}

	public void setTotalStudyCount(int totalStudyCount) {
		this.totalStudyCount = totalStudyCount;
	}

	public int getTotalStudyCount() {
		return totalStudyCount;
	}

	public void setStudyId(String studyId) {
		this.studyId = studyId;
	}

	public String getStudyId() {
		return studyId;
	}

	public void setCellType(String cellType) {
		this.cellType = cellType;
	}

	public String getCellType() {
		return cellType;
	}

	public void setFactor(String factor) {
		this.factor = factor;
	}

	public String getFactor() {
		return factor;
	}

	public void setStudyIds(List<String[]> studyIds) {
		this.studyIds = studyIds;
	}

	public List<String[]> getStudyIds() {
		return studyIds;
	}

	public void setTaxonid(Integer taxonid) {
		this.taxonid = taxonid;
	}

	public Integer getTaxonid() {
		return taxonid;
	}

	public String execute() {
		return "SUCCESS";
	}

	public String fetchAllCellTypes() {
		setCellTypes(getSampleStudyDAO().fetchAllCellTypes());
		return Action.SUCCESS;
	}

	public String fetchAllFactors() {
		setFactorSet(getSampleStudyDAO().fetchAllFactors());
		return Action.SUCCESS;
	}

	public String fetchAllStudyIds() {
		setStudyIds(getSampleStudyDAO().fetchAllStudyIdWithTitle(getTaxonid()));
		return Action.SUCCESS;
	}


	public String fetchSample() {
		LinkedList<Sample_JS> jsa = new LinkedList<Sample_JS>();
		for (Sample s : getSampleStudyDAO().fetchSample(getStudyId(),
				getTaxonid(), getCellType(), getFactor())) {
			Sample_JS saj = new Sample_JS();
			try {
				BeanUtils.copyProperties(saj, s);
				jsa.add(saj);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		setSamples(jsa);
		return Action.SUCCESS;
	}


}
